<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

require_once 'eleontev/SQL/ModifySql.php'; 


class AppModel extends BaseModel
{
  		
	var $delete_mode = 1; // or 2 (1- delete where id IN (1), 2- delete where id IN (1,2,3)) 
	var $id_field = 'id';
	
		
	function checkPriv(&$priv, $action) {
		$priv->check($action);
	}
	
	
	// could be used in PHP5 for to interact with obj classes
	//function __call($method, $args){
	//	return call_user_func_array(array($this->obj, $method), $args);
	//}
	
	
	// COMMON DB ACTIONS & SQL // ---------------------
	
	function getById($record_id, $from_get_records = false) {
		
		// sql will be from getRecordsSql()
		if($from_get_records) {
			$this->setSqlParams(sprintf("AND %s=%d", $this->id_field, $record_id));
			$sql = $this->getRecordsSql();	
		
		} else {
			$sql = "SELECT * FROM {$this->tbl->table} WHERE {$this->sql_params} AND {$this->id_field} = %d";
			$sql = sprintf($sql, $record_id);
		}
		
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->FetchRow();
	}
	
	
	function &getRecords($limit = -1, $offset = -1) {
		$sql = $this->getRecordsSql();
		
		if($limit == -1) {
			$result =& $this->db->Execute($sql) or die(db_error($sql));
		} else {
			$result =& $this->db->SelectLimit($sql, $limit, $offset) or die(db_error($sql));
		}
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		//echo "<pre>"; print_r($result); echo "</pre>";
		//echo "<pre>"; print_r($result->GetArray()); echo "</pre>";
		
		return $result->GetArray();
	}
	
	
	function getRecordsSql() {
		$sql = "SELECT * FROM {$this->tbl->table} WHERE {$this->sql_params} {$this->sql_params_order}";
		return $sql;
	}
	
	
	function getCountRecordsSql() {
		return $this->getRecordsSql();
	}
	
	
	function getCountRecords() {
		$sql = $this->getCountRecordsSql();
		$data =  $this->db->GetCol($sql) or die(db_error($sql));
		return $data[0];
	}
	
	
	function add(&$obj) {
		$sql = ModifySql::getSql('INSERT', $this->tbl->table, $obj->get());
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		//exit();
		
		$this->db->Execute($sql) or die(db_error($sql));
		return $this->db->Insert_ID(); 
	}
	
	
	function update(&$obj) {
		$sql = ModifySql::getSql('UPDATE', $this->tbl->table, $obj->get(), false, $this->id_field);
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		//exit();
		
		$this->db->Execute($sql) or die(db_error($sql));
		return $this->db->Insert_ID();
	}
	
	
	function save(&$obj) {
		$sql = ModifySql::getSql('REPLACE', $this->tbl->table, $obj->get());
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		//exit();
		
		$this->db->Execute($sql) or die(db_error($sql));
		return $this->db->Insert_ID();
	}	
	
	
	function status($value, $id, $field = 'active') {
		$sql = "UPDATE {$this->tbl->table} SET $field='%s' WHERE {$this->id_field} IN (%s)";
		$sql = sprintf($sql, mysql_escape_string($value), $this->idToString($id));
		$this->db->Execute($sql) or die(db_error($sql));
	}
	
	
	function delete($record_id) {
		
		// convert to string 1,2,3... to use in IN()
		$record_id = $this->idToString($record_id);
		
		$sql = "DELETE FROM {$this->tbl->table} WHERE {$this->id_field} IN (%s)";
		$sql = sprintf($sql, $record_id);
		return $this->db->Execute($sql) or die(db_error($sql));
	}
	
	
	
	// UTIL TO GENERATE SQL // ---------------------
	
	// convert to string 1,2,3... to use in IN()
	function idToString($val) {
		
		if(is_array($val)) { 
			
			if($this->delete_mode == 2) {
				$val = implode(',', $val); 
			} else {
				exit('WRONG UTIL SQL');
			}
			
		} else { 
			$val = (int) $val; 
		}
		
		return $val;
	}
}
?>